package com.sample.springbootsample.config;

import com.sample.springbootsample.filter.FilterDemo;
import com.sample.springbootsample.interceptor.InterceptorDemo;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 拦截器注册
 * @projectName: spring-boot-sample
 * @description:
 * @author: Adam
 * @create: 2019-12-11 18:10
 **/

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {


    /**
      * 拦截器配置
      * 拒我想拒的
      * @author jie
      * @param
      * @return
      * @date 2019/12/11 22:40
      */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 对所有的请求都要拦截，但是"/getInt"不在拦截的范围之内
        registry.addInterceptor(new InterceptorDemo())
                .addPathPatterns("/**").excludePathPatterns("/getInt");
    }



    /**
      * 过滤器
      * 取我想取的
      * @author jie
      * @param
      * @return
      * @date 2019/12/11 22:40
      */
    @Bean
    public FilterRegistrationBean testFilterRegistration(FilterDemo myTestFilter) {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(myTestFilter);//添加过滤器
        registration.addUrlPatterns("/*");//设置过滤路径，/*所有路径
        //registration.addInitParameter("name", "alue");//添加默认参数
        registration.setName("MyFilter");//设置优先级
        registration.setOrder(1);//设置优先级
        return registration;
    }
}
